/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseContactDetailsDao.cs
*        创建系统时间:       2012/11/2 19:06:59
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System;
    using System.Data;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseContactDetailsDao : BaseDao, IBaseDao
    {
        public BaseContactDetailsDao()
        {
            base.CurrentTableName = BaseContactDetailsTable.TableName;
        }

        public BaseContactDetailsDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseContactDetailsDao(BaseUserInfo userInfo)
            : this()
        {
            base.UserInfo = userInfo;
        }

        public BaseContactDetailsDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public string Add(BaseContactDetailsEntity baseContactDetailsEntity)
        {
            return this.AddEntity(baseContactDetailsEntity);
        }

        public string AddEntity(BaseContactDetailsEntity baseContactDetailsEntity)
        {
            if (string.IsNullOrEmpty(baseContactDetailsEntity.ID) || string.IsNullOrEmpty(baseContactDetailsEntity.SortCode))
            {
                string sequence = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseContactDetailsTable.TableName);
                if (string.IsNullOrEmpty(baseContactDetailsEntity.ID))
                {
                    baseContactDetailsEntity.ID = sequence;
                }
                if (string.IsNullOrEmpty(baseContactDetailsEntity.SortCode))
                {
                    baseContactDetailsEntity.SortCode = sequence;
                }
            }
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginInsert(BaseContactDetailsTable.TableName);
            sqlBuilder.SetValue(BaseContactDetailsTable.FieldID, baseContactDetailsEntity.ID);
            this.SetEntity(sqlBuilder, baseContactDetailsEntity);
            sqlBuilder.SetValue(BaseContactDetailsTable.FieldCreateUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseContactDetailsTable.FieldCreateDate);
            if (sqlBuilder.EndInsert() <= 0)
            {
                return string.Empty;
            }
            return baseContactDetailsEntity.ID;
        }

        public int AddReceivers(string contactID, string[] receiverIDs)
        {
            int num = 0;
            BaseUserDao dao = new BaseUserDao(base.DbHelper, base.UserInfo);
            BaseUserEntity entity = null;
            for (int i = 0; i < receiverIDs.Length; i++)
            {
                if (!this.Exists(BaseContactDetailsTable.FieldContactID, contactID, BaseContactDetailsTable.FieldReceiverID, receiverIDs[i]))
                {
                    entity = dao.GetEntity(receiverIDs[i]);
                    if (entity.Enabled && !entity.DeleteMark)
                    {
                        BaseContactDetailsEntity baseContactDetailsEntity = new BaseContactDetailsEntity
                        {
                            ID = BaseSequenceDao.Instance.NewGuid(),
                            ContactID = contactID,
                            Category = "User",
                            ReceiverID = entity.ID,
                            ReceiverRealName = entity.Realname,
                            IsNew = true,
                            Enabled = true,
                            NewComment = false
                        };
                        this.Add(baseContactDetailsEntity);
                        num++;
                    }
                }
            }
            this.SetReadState(contactID);
            return num;
        }

        public BaseContactDetailsEntity GetEntity(string id)
        {
            return new BaseContactDetailsEntity(this.Get(id));
        }

        public int OnCommnet(string detailsID)
        {
            int num = 0;
            BaseContactDetailsEntity entity = this.GetEntity(detailsID);
            string sqlQuery = "UPDATE ECP_ContactDetails SET NewComment = 1 WHERE (Enabled = 1) AND (IsNew = 0) AND (ContactID = '" + entity.ContactID + "') AND (ID <> '" + detailsID + "')";
            num += base.DbHelper.ExecuteNonQuery(sqlQuery);
            BaseContactDao dao = new BaseContactDao(base.DbHelper, base.UserInfo);
            BaseContactEntity baseContactEntity = dao.GetEntity(entity.ContactID);
            baseContactEntity.CommentDate = DateTime.Now.ToString();
            baseContactEntity.CommentUserID = base.UserInfo.ID;
            baseContactEntity.CommentUserRealName = base.UserInfo.Realname;
            return (num + dao.Update(baseContactEntity));
        }

        private int OnRead(BaseContactDetailsEntity contactDetailsEntity)
        {
            int num = 0;
            contactDetailsEntity.IsNew = false;
            contactDetailsEntity.NewComment = false;
            contactDetailsEntity.LastViewDate = DateTime.Now.ToString();
            contactDetailsEntity.LastViewIP = base.UserInfo.IPAddress;
            num += this.Update(contactDetailsEntity);
            this.SetReadState(contactDetailsEntity.ContactID);
            return num;
        }

        public DataTable Read(string id)
        {
            BaseContactDetailsEntity contactDetailsEntity = new BaseContactDetailsEntity(this.Get(id));
            this.OnRead(contactDetailsEntity);
            BaseContactDao dao = new BaseContactDao(base.DbHelper, base.UserInfo);
            return dao.Get(contactDetailsEntity.ContactID);
        }

        public int RemoveReceiver(string contactID, string receiverID)
        {
            int num = 0;
            num = this.Delete(BaseContactDetailsTable.FieldContactID, contactID, BaseContactDetailsTable.FieldReceiverID, receiverID);
            this.SetReadState(contactID);
            return num;
        }

        private void SetEntity(SQLBuilder sqlBuilder, BaseContactDetailsEntity baseContactDetailsEntity)
        {
            sqlBuilder.SetValue(BaseContactDetailsTable.FieldContactID, baseContactDetailsEntity.ContactID);
            sqlBuilder.SetValue(BaseContactDetailsTable.FieldCategory, baseContactDetailsEntity.Category);
            sqlBuilder.SetValue(BaseContactDetailsTable.FieldReceiverID, baseContactDetailsEntity.ReceiverID);
            sqlBuilder.SetValue(BaseContactDetailsTable.FieldReceiverRealName, baseContactDetailsEntity.ReceiverRealName);
            sqlBuilder.SetValue(BaseContactDetailsTable.FieldIsNew, baseContactDetailsEntity.IsNew);
            sqlBuilder.SetValue(BaseContactDetailsTable.FieldNewComment, baseContactDetailsEntity.NewComment);
            sqlBuilder.SetValue(BaseContactDetailsTable.FieldLastViewIP, baseContactDetailsEntity.LastViewIP);
            sqlBuilder.SetValue(BaseContactDetailsTable.FieldLastViewDate, baseContactDetailsEntity.LastViewDate);
            sqlBuilder.SetValue(BaseContactDetailsTable.FieldEnabled, baseContactDetailsEntity.Enabled);
            sqlBuilder.SetValue(BaseContactDetailsTable.FieldDeleteMark, baseContactDetailsEntity.DeleteMark);
            sqlBuilder.SetValue(BaseContactDetailsTable.FieldSortCode, baseContactDetailsEntity.SortCode);
            sqlBuilder.SetValue(BaseContactDetailsTable.FieldDescription, baseContactDetailsEntity.Description);
            sqlBuilder.SetValue(BaseContactDetailsTable.FieldCreateUserRealName, baseContactDetailsEntity.CreateUserRealName);
        }

        private int SetReadState(string id)
        {
            int num = 0;
            string sqlQuery = " UPDATE ECP_Contact     SET SendCount = (                       SELECT COUNT(*)                         FROM ECP_ContactDetails                        WHERE (Enabled = 1 AND ContactID = '" + id + "'))   WHERE ID = '" + id + "' ";
            num += base.DbHelper.ExecuteNonQuery(sqlQuery);
            sqlQuery = " UPDATE ECP_Contact     SET ReadCount = (                       SELECT COUNT(IsNew)                         FROM ECP_ContactDetails                        WHERE (Enabled = 1 AND ContactID = '" + id + "') AND (IsNew = 0))   WHERE ID = '" + id + "'";
            return (num + base.DbHelper.ExecuteNonQuery(sqlQuery));
        }

        public int Update(BaseContactDetailsEntity baseContactDetailsEntity)
        {
            return this.UpdateEntity(baseContactDetailsEntity);
        }

        public int UpdateEntity(BaseContactDetailsEntity baseContactDetailsEntity)
        {
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginUpdate(BaseContactDetailsTable.TableName);
            this.SetEntity(sqlBuilder, baseContactDetailsEntity);
            sqlBuilder.SetValue(BaseContactDetailsTable.FieldModifyUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseContactDetailsTable.FieldModifyDate);
            sqlBuilder.SetWhere(BaseContactDetailsTable.FieldID, baseContactDetailsEntity.ID);
            return sqlBuilder.EndUpdate();
        }
    }
}